//
//  SNSQLiteTools.m
//  Sina
//
//  Created by Macx on 16/7/27.
//  Copyright © 2016年 LiuZhuo. All rights reserved.
//

#import "SNSQLiteTools.h"
@interface SNSQLiteTools()


@end

@implementation SNSQLiteTools
+ (instancetype)shareSqliteTools
{
    static SNSQLiteTools * _tools = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _tools = [[SNSQLiteTools alloc] init];
    });
    return _tools;
}
// 2.定义数据库对象
static FMDatabase * _db;
+ (void)initialize
{
    // 1.获取沙盒路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [docPath stringByAppendingPathComponent:@"class.sqlite"];
    
    // 1.创建数据库
    // 如果数据库文件不存在, 就会自动创建文件, 如果存在则不会创建
    FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
    _db = db;
    // 2.打开数据库
    if([db open])
    {
        NSLog(@"success");
        // 2.创建表
        /*
         在FMDB中除了查询以外的操作都称之为更新
         */
   NSString  *sql = @"CREATE TABLE IF NOT EXISTS t_class(id INTEGER PRIMARY KEY AUTOINCREMENT, dict BLOB, build TEXT, week TEXT, date TEXT);";
        BOOL success = [db executeUpdate:sql];
        if (success) {
            NSLog(@"create table success");
        }else
        {
            NSLog(@"create table error");
        }
        
    }else
    {
        NSLog(@"error");
    }
}
// 增加
- (BOOL)insertStatus:(NSDictionary *)dict withDict1:(NSMutableDictionary *)dict1{
    NSData  *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:NULL];
    
    NSString * build = dict1[@"build"];
    NSString * week = dict1[@"week"];
    NSString * date = dict1[@"date"];
    // FMDB的executeUpdate方法支持字符串拼接, 但是需要注意: 如果需要拼接字符串, 那么传入的必须是对象
    BOOL success = [_db executeUpdate:@"INSERT INTO t_class(dict, build, week, date) VALUES(?, ?, ?, ?);", data,build,week,date];
    if (success) {
        return YES;
    }else
    {
        return NO;
    }

}

// 查询
- (NSDictionary *)statuses:(NSMutableDictionary * )dict1{
    
  
    FMResultSet *set =nil;
    NSString * build = dict1[@"build"];
    NSString * week = dict1[@"week"];
    NSString * date = dict1[@"date"];

    NSDictionary * dict;
    set  =  [_db executeQuery:@"SELECT * FROM t_class WHERE build = ? AND week = ? AND date = ?;",build,week,date];
     
    while ([set next]) {
        
        NSData * data = [set dataForColumn:@"dict"];
      dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
        
      
    }
      return dict;
    
}
// 删除
- (void)deleteStatus:(NSMutableDictionary *)dict1{
    NSString * build = dict1[@"build"];
    NSString * week = dict1[@"week"];
    NSString * date = dict1[@"date"];
     BOOL success = [_db executeUpdate:@"DELETE FROM t_class WHERE build = ? AND week = ? AND date = ?;",build,week,date];
    if (success) {
        DDLogDebug(@"清除缓存数据成功");

    }else{
         DDLogDebug(@"清除缓存数据失败");
    }
    
}
    








@end
